package com.cc.read;

import com.cc.dao.DBTransToExcell;
import com.cc.utils.JdbcUtil;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.util.List;

/**
 * @User c.c
 * @Since 2022/8/15
 * @Time 9:02
 * @Description 自定义读取表格元素
 */

public class CustomReadTable {


  /**
   * 读取数据库字段 并设置列表名
   * @author C.c
   * @since 2022-08-16 16:02
   * @param
   * @return
  **/

  public ResultSet readcloum(List<DBTransToExcell> dbTransToExcell,String tablename) throws SQLException {

    StringBuilder selectTodo = new StringBuilder("select 1");

    dbTransToExcell.forEach(e->{
      selectTodo.append(",").append(e.getDbField());
      if (e.getExcellColumnName()!=null&&!StringUtils.isEmpty(e.getExcellColumnName())){
        selectTodo.append(" as ").append(e.getExcellColumnName());
      }
    });

    StringBuilder from = new StringBuilder(" from ").append(tablename);
    StringBuilder select = selectTodo.append(from);
    Connection conn = JdbcUtil.getConnection();

    PreparedStatement preparedStatement = conn.prepareStatement(select.toString());
    ResultSet resultSet = preparedStatement.executeQuery();
    return resultSet;
  }


  @Deprecated
  public ResultSet readcloumAgo(List<String>columnName,String tablename) throws SQLException {

    StringBuilder selectTodo = new StringBuilder("select 1");

    columnName.forEach(e->{
      selectTodo.append(",").append(e);
    });

    StringBuilder from = new StringBuilder(" from ").append(tablename);
    StringBuilder select = selectTodo.append(from);
    Connection conn = JdbcUtil.getConnection();

    PreparedStatement preparedStatement = conn.prepareStatement(select.toString());
    ResultSet resultSet = preparedStatement.executeQuery();
    return resultSet;
  }
}
